package com.briup.cms.service.impl;

import com.briup.cms.bean.BaseUser;
import com.briup.cms.dao.BasUserExtendMapper;
import com.briup.cms.dao.BaseLogMapper;
import com.briup.cms.dao.BaseUserMapper;
import com.briup.cms.exception.CustomerException;
import com.briup.cms.service.IBaseUserService;
import com.briup.cms.utils.JwtUtil;
import com.briup.cms.utils.MessageStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * @Description
 * @Author lining
 * @Date 2021/7/17 16:16
 * @Version 1.0.0
 */
@Service
public class BaseUserServiceImpl implements IBaseUserService {
    @Autowired
    private BasUserExtendMapper mapper;
    @Override
    public String login(String username, String password) throws CustomerException {
        BaseUser baseUser = mapper.findBaseUserByUsername(username);
        if(baseUser == null){
            throw new CustomerException(MessageStatus.USER_NOT_EXIST);
        }
        if(!baseUser.getPassword().equals(password)){
            throw  new CustomerException(MessageStatus.USER_LOGIN_ERROR);
        }
        if(baseUser.getStatus() != 0){
            throw new CustomerException(MessageStatus.USER_ACCOUNT_FORBIDDEN);
        }
        HashMap<String, Object> userInfo = new HashMap<>();
        userInfo.put("username",baseUser.getUsername());
        userInfo.put("realname",baseUser.getRealname());
        return JwtUtil.sign(username,userInfo);
    }
}
